/* 
	Title: row

	Este fichero contiene todas las definiciones y constantes para crear una Row, una columna de celdas de un Formuario.
	Conviene ver antes: <Field>, <Form>, <Cell>
*/


ROW_DEFAULT_CONTENTS =
{
	mixture:	function(div_cells)
	{
		return createElement({ type_node: "div", children: div_cells });
	},

	mixture:		function(div_cells)
	{
		return createElement({ type_node: "div", children: div_cells });
	},

	edit:			function(self)
	{
		var div = createElement({ type_node: "a", attributes: { href: "javascript:;" }, children: [ { type_node: "text", text: "Editar" } ] } );
		EH.addEventHandler(div, "click", function() 
		{ 
			if(EH.getActive(EH.getEvent().target))
				 self.edit(); 
		}, 100);

		return div;
	},

	erase:			function(self)
	{
		var div = createElement({ type_node: "a", attributes: { href: "javascript:;" }, children: [ { type_node: "text", text: "Borrar" } ] } );
		EH.addEventHandler(div, "click", function() 
		{ 
			if(EH.getActive(EH.getEvent().target))
				 self.erase(); 
		}, 100);

		return div;
	},

	submit:			function(self)
	{
		var div = createElement({ type_node: "input", attributes: { type: "button" } });
		div.value = "Aplicar Cambios";

		EH.addEventHandler(div, "click", function()
		{ 
			if(EH.getActive(EH.getEvent().target))
				 self.submit(); 
		}, 100);

		return div;
	}		
};


function Row(parent_div, values, fields, parent_form)
{
	// New_row ---> form
	this.parent_form = parent_form;

	this.parent_div = parent_div;
	this.fields = fields;

	if(this.fields)
	{
		this.cells = new Array;
		for(var i = 0; i < fields.length; i++)
			this.cells[i] = new Cell(fields[i], values[i]);
	}

	this.settings =
	{
		data:
		{
			title:			"",
			edit:			true,
			erase:			true,

			url:			"none"
		},

		class_names:
		{
			title:			"",
			edit:			"fr",
			erase:			"fr",

			content:		"",

			submit:			"",
			footer:			""
		},

		contents:
		{
			mixture_out:	ROW_DEFAULT_CONTENTS.mixture,
			mixture_in:		ROW_DEFAULT_CONTENTS.mixture,

			row:			FORM_DEFAULT_CONTENTS.form,

			title:			FORM_DEFAULT_CONTENTS.title,
			edit:			ROW_DEFAULT_CONTENTS.edit,
			erase:			ROW_DEFAULT_CONTENTS.erase,

			submit:			ROW_DEFAULT_CONTENTS.submit,
			footer:			FORM_DEFAULT_CONTENTS.footer
		}
	};


	this.div_title = false;
	this.div_footer = false;
	this.div_submit = false;

	this.div_mixture = false;
	this.div_content = false;

}

Row.prototype = 
{
	show: function(type)
	{
		if(this.parent_div)
		{
			// Mostrar Cells
			var div_cells = new Array;
			for(var i in this.cells)
				div_cells.push(this.cells[i].show(type));

			if(this.div_title)
			{
				this.div_title.parentNode.removeChild(this.div_title);
				this.div_title = false;
			}
			
			if(this.div_footer)
			{
				this.div_footer.parentNode.removeChild(this.div_footer);
				this.div_footer = false;
			}

			if(this.div_submit)
			{
				this.div_submit.parentNode.removeChild(this.div_submit);
				this.div_submit = false;
			}

			if(this.div_content)
			{
				this.div_content.parentNode.removeChild(this.div_content);
				this.div_content = false;
			}				
			
			var edit = false, erase = false;

			if(type == "output")
			{
				if(this.settings.data.edit)
				{
					edit = this.settings.contents.edit(this);
					edit.className = this.settings.class_names.edit;
				}
				if(this.settings.data.erase)
				{
					erase = this.settings.contents.erase(this);
					erase.className = this.settings.class_names.erase;
				}
			}
			// Si Type es IN, crear el Submit
			if(type == "input")
			{
				var self = this;
				this.div_submit = this.settings.contents.submit(self);
				this.div_submit.className = this.settings.class_names.submit;
			}

			this.div_title = this.settings.contents.title(this.settings.data.title, edit, erase);
			this.div_title.className = this.settings.class_names.title;

			this.div_footer = this.settings.contents.footer(this.div_submit);
			this.div_footer.className = this.settings.class_names.footer;

			if(type == "input")
				this.div_mixture = this.settings.contents.mixture_in(div_cells);
			else
				this.div_mixture = this.settings.contents.mixture_out(div_cells);

			this.div_content = this.settings.contents.row(this.div_title, this.div_mixture, this.div_footer);

			var xobj = new XObject(this.div_content);
			xobj.setStyle("opacity", 10);

			this.parent_div.appendChild(this.div_content);
			xobj.setOpacity(100, 20);


		}
	},

	getPrimKeys:		function()
	{
		var pk = new Array;
		for(var c in this.cells)
		{
			if(this.cells[c].field.isPrimKey())
				pk.push({ "name": this.cells[c].field.name, "value": this.cells[c].value });
		}

		return pk;
	},

	setup: function(data)
	{
		var res = "";

		if(data)
		{
			for(var lv1 in data)
			{
				for(var lv2 in data[lv1])
				{

					this.settings[lv1][lv2] = data[lv1][lv2];
				}
			}
		}
	},

	erase:		function()
	{
		var res = JSONstring.make({ type_query: "DELETE", "data": this.getPrimKeys()  });
		new net.ContentLoader(this.settings.data.url, this.update, false, "params=" + res, this);
	},
		

	edit:		function()
	{
		this.show("input");
		

	},

	submit:		function()
	{
		var res = 0;
		for(var i in this.cells)
			res += this.cells[i].check();

		if(res == 0)
		{
			// Insertar
			if(this.parent_form)
			{
				this.parent_form.addRow(this);

				var data = new Array;
				for(var c in this.cells)
				{
					data.push(this.cells[c].input.value);
				}

				var res = JSONstring.make({ type_query: "INSERT", "data": data });
				new net.ContentLoader(this.settings.data.url, this.update, false, "params=" + res, this);
			}
			else
			{
				var changes = new Array;
				var changes_n = 0;
				for(var c in this.cells)
				{
					if(this.cells[c].change)
					{
						changes.push({ "name": this.cells[c].field.name, "value": this.cells[c].input.value });
						changes_n ++;
					}
				}

				if(changes_n > 0)
				{
					var res = JSONstring.make({ type_query: "UPDATE", "data": { "select": this.getPrimKeys(), "update": changes } });
					new net.ContentLoader(this.settings.data.url, this.update, false, "params=" + res, this);
				}
				else
				{
					this.show("output");
				}			
			}	
		}
	},

	update:		function(req)
	{
		var json = eval('(' + req.responseText + ')');

		if(!json.error)
		{
			if(json.type == "update")
			{
				var rd = json.rows_data;
			
				for(var v in rd)
				{
					var found = false;
					var i = 0;
					while(!found && this.cells[i])
					{
						if(this.cells[i].field.name == rd[v].name)
						{
							this.cells[i].value = rd[v].value;
							found = true;
						}
						i++;
					}
				}

				this.show("output");
			}
			else if(json.type == "delete")
			{
				this.destroy();
			}
		}
		else
		{
			show(json.error);	
		}
	},

	destroy:	function()
	{			
		var xobj = new XObject(this.div_content);
		xobj.destroy();
	}		

}
		

	
